package com.qiyun.dinghuohui.base;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.qiyun.dinghuohui.R;
import com.qiyun.dinghuohui.utils.CommonUtil;
import com.qiyun.dinghuohui.utils.InjectUtil;

/**
 * Created by xlm on 2018\5\25 0025
 */

public abstract class BaseDialog extends Dialog implements View.OnClickListener {
    /**
     * 因为Dialog将构造方法内的context转成ContextThemeWrapper，
     * 之后会造成context无法再转成activity，所以此处自己将context赋值给activity
     * 并提供getActivity方法
     */
    private BaseActivity mActivity;

    public BaseDialog(Context context) {
        super(context, R.style.CommonDialog);
        mActivity = (BaseActivity) context;
    }

    public BaseDialog(Context context, int themeResId) {
        super(context, themeResId);
        mActivity = (BaseActivity) context;
    }

    protected BaseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        mActivity = (BaseActivity) context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onSetContentView();
        injectView();
        initViews();
        afterOnCreate(savedInstanceState);
        onSetListener();
    }

    public BaseActivity getThis() {
        return mActivity;
    }

    /**
     * 通过注解自动绑定资源ID
     */
    private void injectView() {
        InjectUtil.injectDialogFields(this);
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {

    }

    protected abstract void onSetContentView();

    protected abstract void initViews();

    protected abstract void onSetListener();

    protected abstract void afterOnCreate(Bundle savedInstanceState);


    /**
     * @param widthscale  宽度占屏幕宽度比例
     * @param heightscale 宽度占屏幕宽度比例
     * @param gravity
     */
    public void initWindow(float widthscale, float heightscale, int gravity) {
        initWindow(widthscale, heightscale, gravity, true);
    }

    /**
     * @param widthscale  宽度占屏幕宽度比例
     * @param heightscale 宽度占屏幕宽度比例
     */
    public void initWindow(float widthscale, float heightscale) {
        initWindow(widthscale, heightscale, Gravity.CENTER, true);
    }

    /**
     * @param widthscale  宽度占屏幕宽度比例
     * @param heightscale 宽度占屏幕宽度比例
     * @param gravity
     * @param outcancel   是否点击外部消失
     */
    private void initWindow(float widthscale, float heightscale, int gravity, boolean outcancel) {
        Window window = getWindow();
        WindowManager.LayoutParams lp = null;
        if (window != null) {
            lp = window.getAttributes();
            if (gravity == Gravity.NO_GRAVITY) {
                window.setGravity(Gravity.CENTER);
            } else {
                window.setGravity(gravity);
            }
            window.setBackgroundDrawableResource(R.drawable.shape_transparent);
        }
        setCanceledOnTouchOutside(outcancel);
        if (lp != null) {
            if (widthscale > 0) {
                int width = CommonUtil.getDisplayMetrics(getContext()).widthPixels;
                lp.width = (int) (widthscale * width);
            }
            if (heightscale > 0) {
                int height = CommonUtil.getDisplayMetrics(getContext()).heightPixels;
                lp.height = (int) (heightscale * height);
            }
            window.setAttributes(lp);
        }

    }

    public void setGravity(int gravity) {
        Window window = getWindow();
        if (window != null) {
            if (gravity == Gravity.NO_GRAVITY) {
                window.setGravity(Gravity.CENTER);
            } else {
                window.setGravity(gravity);
            }
        }
    }

    public void setAnimate(@StyleRes int id) {
        Window window = getWindow();
        if (window != null) {
            window.setWindowAnimations(id);
        }
    }

    public void showToast(String msg) {
        mActivity.showToast(msg);
    }

    public void showLoading(String msg) {
        mActivity.showLoading(msg);
    }

    public void dismissLoading() {
        mActivity.dismissLoading();
    }
}
